import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/gen/assets.gen.dart';
import 'package:sgy_flutter/model/dls_upload_model.dart';
import 'package:sgy_flutter/model/dls_upload_model_old.dart';
import 'package:sgy_flutter/page/detail/logic.dart';
import 'package:sgy_flutter/page/detail/widget/ble_state_view.dart';
import 'package:sgy_flutter/page/detail/widget/ble_stream_view.dart';

import '../widget/device_data_view.dart';
import 'package:rxdart/rxdart.dart' as rx;

class OldDlsDoubleCenterView extends GetView<DetailLogic> {
  const OldDlsDoubleCenterView({super.key});
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          width: 0,
          color: Get.theme.primaryColor,
        ),
        color: Get.theme.primaryColor,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 功率,踏频,左右平衡
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(28), topRight: Radius.circular(28)),
              border: Border(
                  bottom: BorderSide(color: Get.theme.primaryColor, width: 6)),
            ),
            padding: EdgeInsets.only(top: 24, bottom: 10, left: 12, right: 12),
            child: BleStateView(
              bleManager: controller.bleManager,
              builder: (connected) {

                if (controller
                    .bleManager.cyclingPowerService16.isSupportNotify) {
                  return StreamBuilder(
                      stream: controller.bleManager.cyclingPowerService16.resp.stream,
                      builder: (context, snapshot) {
                        final byteData = snapshot.data;
                        String? power;
                        String? cadence;
                        String? leftRightBalance;
                        String? cadenceSmooth;
                        String? torsionAvailability;
                        if (byteData != null && connected) {
                          power = byteData.power;
                          cadence = byteData.cadence;
                          leftRightBalance = byteData.balance;
                        }
                        return _build(
                            power: power,
                            cadence: cadence,
                            leftRightBalance: leftRightBalance,
                            cadenceSmooth: cadenceSmooth,
                            torsionAvailability: torsionAvailability);
                      });
                }
                if (controller.bleManager.cyclingPowerService.isSupportNotify) {
                  return StreamBuilder(
                      stream: controller.bleManager.cyclingPowerService.resp.stream,
                      builder: (context, snapshot) {
                        final byteData = snapshot.data;
                        String? power;
                        String? cadence;
                        String? leftRightBalance;
                        String? cadenceSmooth;
                        String? torsionAvailability;
                        if (byteData != null && connected) {
                          power = byteData.power;
                          cadence = byteData.cadence;
                          leftRightBalance = byteData.balance;
                        }
                        return _build(
                            power: power,
                            cadence: cadence,
                            leftRightBalance: leftRightBalance,
                            cadenceSmooth: cadenceSmooth,
                            torsionAvailability: torsionAvailability);
                      });
                }

                return StreamBuilder(
                    stream: controller.dlsUploadDataOld,
                    builder: (context, snapshot) {
                      final data = snapshot.data;
                      final l = data?.first;
                      final r = data?.second;
                      String? power;
                      String? cadence;
                      String? leftRightBalance;
                      String? cadenceSmooth;
                      String? torsionAvailability;
                      if (l != null && r != null && connected) {
                        power = "${(l.power ?? 0) + (r.power ?? 0)}";
                        cadence = "${((l.cadence ?? 0) + (r.cadence ?? 0)) ~/ 2}";
                        if ((l.power ?? -1) + (r.power ?? -1) == 0) {
                          leftRightBalance = "50-50";
                        } else {
                          final total = (l.power ?? 0) + (r.power ?? 0);
                          leftRightBalance =
                          "${(l.power ?? 0) * 100 ~/ total}-${(r.power ?? 0) * 100 ~/ total}";
                        }
                        cadenceSmooth =
                        "${min((l.cadenceSmooth ?? 0), 100)}-${min((r.cadenceSmooth ?? 0), 100)}";
                        torsionAvailability =
                        "${min((l.torsionAvailability ?? 0), 100)}-${min((r.torsionAvailability ?? 0), 100)}";
                      }
                      return _build(
                          power: power,
                          cadence: cadence,
                          leftRightBalance: leftRightBalance,
                          cadenceSmooth: cadenceSmooth,
                          torsionAvailability: torsionAvailability);
                    });
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _build(
      {required String? power,
        required String? cadence,
        required String? leftRightBalance,
        required String? cadenceSmooth,
        required String? torsionAvailability}) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            DeviceDataView(
                imagePath: Assets.image.iconLightning.path,
                label: "功率/W".tr,
                value: power ?? '--'),
            Container(
              width: 2,
              color: Get.theme.dividerColor,
              height: 50,
            ),
            DeviceDataView(
                imagePath: Assets.image.iconFrequency.path,
                label: "踏频/RPM".tr,
                value: cadence ?? '--'),
          ],
        ),
        Row(
          // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            DeviceDataView(
              imagePath: null,
              label: "左右平衡/%".tr,
              value: leftRightBalance ?? '--',
            ),
            Container(
              width: 2,
              color: Get.theme.dividerColor,
              height: 50,
            ),
            DeviceDataView(
                imagePath: null,
                label: "踩踏平顺度/%".tr,
                value: cadenceSmooth ?? '--'),
            Container(
              width: 2,
              color: Get.theme.dividerColor,
              height: 50,
            ),
            DeviceDataView(
                imagePath: null,
                label: "扭矩有效性/%".tr,
                value: torsionAvailability ?? '--'),
          ],
        ).marginOnly(top: 12, bottom: 12)
      ],
    );
  }


}
